#!/bin/bash

BASEDIR=`dirname $0`
CURRY_WORKDIR=${BASEDIR}/work

oneTimeSetUp()
{
    rm -Rf ${CURRY_WORKDIR}
}

#run once before each test
setUp()
{
    echo "************************************************"
    mkdir -p ${CURRY_WORKDIR}
}
#run once after each test
tearDown()
{
    echo "tearing down"
    rm -Rf ${CURRY_WORKDIR}
}
testEquality()
{
    #           Message             expected   actual
    assertEquals "a-ok"               1            1
}

LIBMOONBUCKS=${BASEDIR}/libmoonbucks
LIBMOONBUCKS_WORKDIR=${CURRY_WORKDIR}/libmoonbucks
LIBDULL=${BASEDIR}/libdull
LIBDULL_WORKDIR=${CURRY_WORKDIR}/libdull

USERVARS=${BASEDIR}/uservars

testGenerateSimpleStaticLibrary()
{
    FILES="${LIBMOONBUCKS}/moonbucks.cpp \
           ${LIBMOONBUCKS}/moonbucks.h"
    pushd ${CURRY_WORKDIR} && curry -init && popd
    cp -f ${FILES} ${CURRY_WORKDIR}/.
    cp -f ${USERVARS}/uservars.Makefile.1 ${CURRY_WORKDIR}/uservars.Makefile

    pushd ${CURRY_WORKDIR} && make && popd
    assertTrue "file libmoonbucks.a not generated" "[ -f ${CURRY_WORKDIR}/libmoonbucks.a ]"
}
testGenerateSimpleStaticLibrary_ensureno_recurry()
{
    FILES="${LIBMOONBUCKS}/moonbucks.cpp \
           ${LIBMOONBUCKS}/moonbucks.h"
    pushd ${CURRY_WORKDIR} && curry -init && popd
    cp -f ${FILES} ${CURRY_WORKDIR}/.
    cp -f ${USERVARS}/uservars.Makefile.1 ${CURRY_WORKDIR}/uservars.Makefile

    pushd ${CURRY_WORKDIR} && make && popd

    assertTrue "file libmoonbucks.a not generated" "[ -f ${CURRY_WORKDIR}/libmoonbucks.a ]"

    pushd ${CURRY_WORKDIR}
    make > output
    cat output |grep -i "Creating"
    if [ $? -eq 0 ]; then
	fail "we should not have to run curry since uservars.Makefile didn't change"
    fi
    popd

}

testGenerateSimpleStaticLibraryUsingDashC()
{
    FILES="${LIBMOONBUCKS}/moonbucks.cpp \
           ${LIBMOONBUCKS}/moonbucks.h"
    pushd ${CURRY_WORKDIR} && curry -init && popd
    cp -f ${FILES} ${CURRY_WORKDIR}/.
    cp -f ${USERVARS}/uservars.Makefile.1 ${CURRY_WORKDIR}/uservars.Makefile

    make -C ${CURRY_WORKDIR}
    assertTrue "file libmoonbucks.a not generated" "[ -f ${CURRY_WORKDIR}/libmoonbucks.a ]"
}

testGenerateSimpleStaticLibraryUsingDashF()
{
    FILES="${LIBMOONBUCKS}/moonbucks.cpp \
           ${LIBMOONBUCKS}/moonbucks.h"
    pushd ${CURRY_WORKDIR} && curry -init && popd
    cp -f ${FILES} ${CURRY_WORKDIR}/.
    cp -f ${USERVARS}/uservars.Makefile.1 ${CURRY_WORKDIR}/uservars.Makefile

    make -f ${CURRY_WORKDIR}/Makefile
    assertTrue "file libmoonbucks.a not generated" "[ -f ${CURRY_WORKDIR}/libmoonbucks.a ]"
}

testGenerateSimpleStaticLibraryUsingDashCandDashF()
{
    FILES="${LIBMOONBUCKS}/moonbucks.cpp \
           ${LIBMOONBUCKS}/moonbucks.h"
    pushd ${CURRY_WORKDIR} && curry -init && popd
    cp -f ${FILES} ${CURRY_WORKDIR}/.
    cp -f ${USERVARS}/uservars.Makefile.1 ${CURRY_WORKDIR}/uservars.Makefile

    make -C ${CURRY_WORKDIR} -f Makefile
    assertTrue "file libmoonbucks.a not generated" "[ -f ${CURRY_WORKDIR}/libmoonbucks.a ]"
}



testGenerateStaticLibraryAndAExecutableThatHasDependencyonStaticLib_ensure_norebuild()
{
    FILES="${LIBMOONBUCKS}/moonbucks.cpp \
           ${LIBMOONBUCKS}/moonbucks.h   \
           ${LIBMOONBUCKS}/main.cpp"
    pushd ${CURRY_WORKDIR} && curry -init && popd
    cp -f ${FILES} ${CURRY_WORKDIR}/.
    cp -f ${USERVARS}/uservars.Makefile.2 ${CURRY_WORKDIR}/uservars.Makefile

    pushd ${CURRY_WORKDIR} && make && popd
    assertTrue "file libmoonbucks.a not generated" "[ -f ${CURRY_WORKDIR}/libmoonbucks.so ]"
    assertTrue "file moonbucksstore not generated" "[ -f ${CURRY_WORKDIR}/moonbucksstore ]"
    pushd ${CURRY_WORKDIR} && make run > .output && popd
    cat ${CURRY_WORKDIR}/.output |grep "Moonbucks constructor called"
    RETVAL=$?
    assertEquals "moonbucks wasn't properly run" 0 ${RETVAL}

    pushd ${CURRY_WORKDIR}
    EXT_EXEC_DEPS_MAIN=`make lookupgenvar V=EXTERNAL_EXEC_DEPS_main`
    LCL_EXEC_DEPS_MAIN=`make lookupgenvar V=LOCAL_EXEC_DEPS_main`
    GENLIBPATH=`make lookuputilsvar V=GENLDLIBRARY`
    popd
    assertEquals "external executable deps invalid" "" "${EXT_EXEC_DEPS_MAIN}"
    assertEquals "local executable deps invalid" "libmoonbucks.so" "${LCL_EXEC_DEPS_MAIN}"
    assertEquals "LD_LIBRARY_PATH not setup correctly" "LD_LIBRARY_PATH=:." ${GENLIBPATH}

    pushd ${CURRY_WORKDIR}
    make > .output1
    cat .output |grep "g++"
    if [ $? -eq 0 ]; then
	fail "we shouldn't have to rebuild anything!"
    fi
    touch libmoonbucks.so
    make > .output2
    cat .output2 |grep "g++"
    if [ $? -ne 0 ]; then
	fail "We should have rebuilt !"
    fi
    popd
}

testSimpleCurry_2_internal_link_dependency(){
    FILES="${LIBMOONBUCKS}/moonbucks.cpp \
           ${LIBMOONBUCKS}/moonbucks.h \
           ${LIBMOONBUCKS}/main.cpp"
    cp ${FILES} ${CURRY_WORKDIR}/.

    pushd ${CURRY_WORKDIR} && curry -init && popd
    cp -f ${USERVARS}/uservars.Makefile_internal_link_dependency ${CURRY_WORKDIR}/uservars.Makefile

    pushd ${CURRY_WORKDIR} && make && popd
    assertTrue "file libmoonbucks.a not generated" "[ -f ${CURRY_WORKDIR}/libmoonbucks.so ]"
    assertTrue "file moonbucksstore not generated" "[ -f ${CURRY_WORKDIR}/moonbucksstore ]"
    pushd ${CURRY_WORKDIR} && make run > .output && popd
    cat ${CURRY_WORKDIR}/.output |grep "Moonbucks constructor called"
    RETVAL=$?
    assertEquals "moonbucks wasn't properly run" 0 ${RETVAL}

    pushd ${CURRY_WORKDIR}
    EXT_EXEC_DEPS_MAIN=`make lookupgenvar V=EXTERNAL_EXEC_DEPS_main`
    LCL_EXEC_DEPS_MAIN=`make lookupgenvar V=LOCAL_EXEC_DEPS_main`
    GENLIBPATH=`make lookuputilsvar V=GENLDLIBRARY`
    popd
    assertEquals "external executable deps invalid" "" "${EXT_EXEC_DEPS_MAIN}"
    assertEquals "local executable deps invalid" "libmoonbucks.so" "${LCL_EXEC_DEPS_MAIN}"
    assertEquals "LD_LIBRARY_PATH not setup correctly" "LD_LIBRARY_PATH=:." ${GENLIBPATH}

}

OS_DETECT=`uname`
if [ x"${OS_DETECT}" != x"Darwin" ]; then

DISABLED_testCrossCompiler(){
    which powerpc-440-linux-gnu-g++
    if [ $? -ne 0 ]; then
	fail "powerpc-440-gnu-linux-g++ compiler not found :("
	return
    fi
    which powerpc-440-linux-gnu-ar
    if [ $? -ne 0 ]; then
	fail "powerpc-440-gnu-linux-ar compiler not found :("
	return
    fi

    FILES="${LIBMOONBUCKS}/moonbucks.cpp \
           ${LIBMOONBUCKS}/moonbucks.h \
           ${LIBMOONBUCKS}/main.cpp"
    cp ${FILES} ${CURRY_WORKDIR}/.
    pushd ${CURRY_WORKDIR} && curry -init && popd
    cp -f ${USERVARS}/uservars.Makefile.crosscompiler ${CURRY_WORKDIR}/uservars.Makefile

    pushd ${CURRY_WORKDIR} && make && popd
    assertTrue "file libmoonbucks.a not generated" "[ -f ${CURRY_WORKDIR}/libmoonbucks.a ]"
    assertTrue "file moonbucksstore not generated" "[ -f ${CURRY_WORKDIR}/moonbucksstore ]"
    file ${CURRY_WORKDIR}/moonbucksstore |grep PowerPC
    RETVAL=$?
    assertEquals "moonbucks wasn't compiled with the powerpc compiler" 0 ${RETVAL}


}

DISABLED_testUseNativeAndCross(){
    which powerpc-440-linux-gnu-g++
    if [ $? -ne 0 ]; then
	fail "powerpc-440-gnu-linux-g++ compiler not found :("
	return
    fi
    which powerpc-440-linux-gnu-ar
    if [ $? -ne 0 ]; then
	fail "powerpc-440-gnu-linux-ar compiler not found :("
	return
    fi

    FILES="${LIBMOONBUCKS}/moonbucks.cpp \
           ${LIBMOONBUCKS}/moonbucks.h \
           ${LIBMOONBUCKS}/main.cpp"
    cp ${FILES} ${CURRY_WORKDIR}/.
    pushd ${CURRY_WORKDIR} && curry -init && popd
    cp -f ${USERVARS}/uservars.Makefile.UseNativeAndCross ${CURRY_WORKDIR}/uservars.Makefile

    pushd ${CURRY_WORKDIR} && make && popd
    assertTrue "file libmoonbucks.a not generated" "[ -f ${CURRY_WORKDIR}/objs/mainpowerpc/moonbucksstore ]"
    assertTrue "file moonbucksstore not generated" "[ -f ${CURRY_WORKDIR}/objs/main/moonbucksstore ]"
    assertTrue "file libmoonbucks.a not generated" "[ -f ${CURRY_WORKDIR}/objs/libmoonbuckspowerpc/libmoonbucks.a ]"
    assertTrue "file moonbucksstore not generated" "[ -f ${CURRY_WORKDIR}/objs/libmoonbucks/libmoonbucks.a ]"

    file ${CURRY_WORKDIR}/objs/mainpowerpc/moonbucksstore |grep "PowerPC"
    RETVAL=$?
    assertEquals "moonbucks wasn't compiled with the powerpc compiler" 0 ${RETVAL}

    #Yes, lets assume right now our build machine is Intel 80386
    file ${CURRY_WORKDIR}/objs/main/moonbucksstore |grep "Intel\ 80386"
    RETVAL=$?
    assertEquals "moonbucks wasn't compiled with the powerpc compiler" 0 ${RETVAL}

    pushd ${CURRY_WORKDIR}
    EXT_DEPS_MAIN=`make lookupgenvar V=EXTERNAL_EXEC_DEPS_main`
    LCL_DEPS_MAIN=`make lookupgenvar V=LOCAL_EXEC_DEPS_main`

    EXT_DEPS_MAINPPC=`make lookupgenvar V=EXTERNAL_EXEC_DEPS_mainpowerpc`
    LCL_DEPS_MAINPPC=`make lookupgenvar V=LOCAL_EXEC_DEPS_mainpowerpc`

    popd
    assertEquals "external executable deps invalid" "" "${EXT_DEPS_MAIN}"
    assertEquals "local executable deps invalid" "objs/libmoonbucks/libmoonbucks.a" "${LCL_DEPS_MAIN}"

    assertEquals "external executable deps invalid" "" "${EXT_DEPS_MAINPPC}"
    assertEquals "local executable deps invalid" "objs/libmoonbuckspowerpc/libmoonbucks.a" "${LCL_DEPS_MAINPPC}"

}

fi

testSimpleCurry_wexlibdep(){
    mkdir ${LIBMOONBUCKS_WORKDIR}
    mkdir ${LIBDULL_WORKDIR}
    FILES="${LIBMOONBUCKS}/moonbucks.cpp \
           ${LIBMOONBUCKS}/moonbucks.h \
           ${LIBMOONBUCKS}/mainwexlibdep.cpp"
    cp ${FILES} ${LIBMOONBUCKS_WORKDIR}/.
    FILES="${LIBDULL}/dull.cpp \
           ${LIBDULL}/dull.h \
           ${LIBDULL}/Makefile"
    cp ${FILES} ${LIBDULL_WORKDIR}/.

    pushd ${LIBDULL_WORKDIR}      &&  make && popd
    assertTrue "file libdull.a not generated" "[ -f ${LIBDULL_WORKDIR}/libdull.a ]"
    pushd ${LIBMOONBUCKS_WORKDIR} &&  curry -init && popd
    cp -f ${USERVARS}/uservars.Makefile.wexlibdep ${LIBMOONBUCKS_WORKDIR}/uservars.Makefile

    pushd ${LIBMOONBUCKS_WORKDIR} && make && popd
    assertTrue "file libmoonbucks.a not generated" "[ -f ${LIBMOONBUCKS_WORKDIR}/libmoonbucks.a ]"
    assertTrue "file moonbucksstore not generated" "[ -f ${LIBMOONBUCKS_WORKDIR}/mainexec ]"
    pushd ${LIBMOONBUCKS_WORKDIR}
    EXT_EXEC_DEPS_MAIN=`make lookupgenvar V=EXTERNAL_EXEC_DEPS_main`
    LCL_EXEC_DEPS_MAIN=`make lookupgenvar V=LOCAL_EXEC_DEPS_main`
    popd
    assertEquals "external executable deps invalid" "../libdull/libdull.a" "${EXT_EXEC_DEPS_MAIN}"
    assertEquals "local executable deps invalid" "libmoonbucks.a" "${LCL_EXEC_DEPS_MAIN}"
}
testSimpleCurry_internal_link_dependency_with_strip_and_clean(){
    FILES="${LIBMOONBUCKS}/moonbucks.cpp \
           ${LIBMOONBUCKS}/moonbucks.h \
           ${LIBMOONBUCKS}/main.cpp"
    cp ${FILES} ${CURRY_WORKDIR}/.

    pushd ${CURRY_WORKDIR} && curry -init && popd
    cp -f ${USERVARS}/uservars.Makefile_internal_link_dependency_with_strip_and_clean ${CURRY_WORKDIR}/uservars.Makefile

    pushd ${CURRY_WORKDIR} && make && popd
    assertTrue "file libmoonbucks.a not generated" "[ -f ${CURRY_WORKDIR}/libmoonbucks.so ]"
    assertTrue "file moonbucksstore not generated" "[ -f ${CURRY_WORKDIR}/moonbucksstore ]"
    assertTrue "file moonbucksstore not generated" "[ -f ${CURRY_WORKDIR}/moonbucksstore_stripped ]"

    pushd ${CURRY_WORKDIR} && make clean && popd
    assertTrue "file libmoonbucks.a not removed" "[ ! -f ${CURRY_WORKDIR}/libmoonbucks.so ]"
    assertTrue "file moonbucksstore not removed" "[ ! -f ${CURRY_WORKDIR}/moonbucksstore ]"
    assertTrue "file moonbucksstore not removed" "[ ! -f ${CURRY_WORKDIR}/moonbucksstore_stripped ]"
}





source ${BASEDIR}/shunit2-2.1.5/src/shell/shunit2
